package com.example.stock.util;

import com.alibaba.fastjson.JSON;
import com.example.stock.vo.ScoreVo;
import com.example.stock.vo.TushareRspVo;

import java.lang.reflect.Method;
import java.math.BigDecimal;
import java.math.RoundingMode;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.function.Function;
import java.util.logging.Handler;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import java.util.stream.Collectors;

public class CommUtil {

    //评分
    public static double score(ScoreVo vo) {
        double returnRateYearMy = vo.getReturnRateYearMy();
        double drawdownMe = vo.getDrawdownMe();
        double returnRateYearRelative = vo.getReturnRateYearRelative();
        double score = 100.0 + drawdownMe + returnRateYearMy*0.1 + 0.5*returnRateYearRelative;
        vo.setScore(score);
        vo.double3();
        return score;
    }

    //小数设置
    public static double double3(double score) {
        return new BigDecimal(score).setScale(3, RoundingMode.HALF_UP).doubleValue();
    }

    public static  <T> List<T> toList(TushareRspVo tushareRspVo, Class<T> tClass) {
        TushareRspVo.DataBean data = tushareRspVo.getData();
        List<String> fields = data.getFields();
        List<List<String>> items = data.getItems();

        Function<List<String>, T> listStringFunction = strings -> {

            Map<String, String> map = new HashMap<>();
            for (int i = 0; i < fields.size(); i++) {
                String field = fields.get(i);
                String value = strings.get(i);
                map.put(field, value);

            }
            String str = JSON.toJSONString(map);
            return JSON.parseObject(str, tClass);
        };


        List<T> collect = items.stream().map(listStringFunction).collect(Collectors.toList());
        return collect;

    }

    private static Pattern linePattern = Pattern.compile("_(\\w)");


    /** 下划线转驼峰 */
    public static String lineToHump(String str) {
        str = str.toLowerCase();
        Matcher matcher = linePattern.matcher(str);
        StringBuffer sb = new StringBuffer();
        while (matcher.find()) {
            matcher.appendReplacement(sb, matcher.group(1).toUpperCase());
        }
        matcher.appendTail(sb);
        return sb.toString();
    }

    public static String getActualName(String underLineString){
        String name = lineToHump(underLineString);
        return "set" + name.substring(0, 1).toUpperCase() + name.substring(1);

    }
}
